import { toSuccess, useEntityManager } from "@sker/core"
import { KsLiveRoomDayLog, KsLiveRoomLog } from "@sker/entities"
import { useData } from "@sker/runtime"
import dayjs from "dayjs"
import { MoreThan } from "typeorm"
import { z } from "zod"

/**
 * 获取某设备 今日统计信息
 */
export default async () => {
    const input = z.object({
        uuid: z.string()
    })
    const body = input.parse(useData())
    await useEntityManager(async m => {
        const now = new Date();
        const year = `${now.getFullYear()}`
        const month = `${now.getMonth()}`
        const day = `${now.getDate()}`
        const todayList = await m.find(KsLiveRoomDayLog, { where: { uuid: body.uuid, year, month, day } })
        const ksItem = todayList.find(it => it.pkg === 'com.smile.gifmaker')
        const jsItem = todayList.find(it => it.pkg === 'com.kuaishou.nebula')
        const link = await m.count(KsLiveRoomLog, { where: { create_date: MoreThan(dayjs().startOf('day').toDate()) } })
        return toSuccess({
            ks: ksItem?.count || 0,
            js: jsItem?.count || 0,
            link
        })
    })
    return toSuccess({
        ks: 0,
        js: 0,
        link: 0
    })
}